﻿Imports Microsoft.Office.Interop
Imports BUS, DTO
Public Class frmLuuThongTinDocGia

    Private Sub frmLuuThongTinDocGia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim docgiaBUS As New DocGiaBUS()
        dtgvdsdocgia.DataSource = docgiaBUS.LayDSDocGiaTable()
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try
            Dim sfdl As New SaveFileDialog()
            sfdl.Filter = " All file *.* | *.*"

            If sfdl.ShowDialog() = DialogResult.OK Then
                Dim path As String = sfdl.FileName
                Dim xAL As Excel.Application
                Dim xWB As Excel.Workbook
                Dim xWS As Excel.Worksheet

                xAL = CreateObject("Excel.Application")
                xWB = xAL.Workbooks.Add()
                'xWB.Sheets.Add()
                xWS = xWB.ActiveSheet

                xWS.Cells(1, 1) = "MaDocGia"
                xWS.Cells(1, 2) = "TenDocGia"
                xWS.Cells(1, 3) = "MaLoaiDocGia"
                xWS.Cells(1, 4) = "NgaySinh"
                xWS.Cells(1, 5) = "DiaChi"
                xWS.Cells(1, 6) = "Email"
                xWS.Cells(1, 7) = "NgayLapThe"
                xWS.Cells(1, 8) = "MaNhanVien"


                For i As Integer = 0 To dtgvdsdocgia.RowCount - 2
                    For j As Integer = 1 To 8
                        xWS.Cells(i + 2, j) = dtgvdsdocgia.Item(j - 1, i).Value
                    Next
                Next


                xAL.ActiveWorkbook.SaveAs(path)

                MessageBox.Show("Lưu thành công !")
            End If
        Catch ex As Exception
            MessageBox.Show("Lưu thất bại !")
        End Try
    End Sub
End Class